package com.feather.common.data.simple;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import com.alibaba.fastjson.JSON;
import com.feather.common.data.DataColumn;
import com.feather.common.data.DataColumnCollection;
import com.feather.common.data.DataItemCollection;
import com.feather.common.data.DataRow;
import com.feather.common.data.DataRowCollection;
import com.feather.common.data.DataSet;
import com.feather.common.data.DataTable;
import com.feather.common.data.DataTableCollection;

/**
 * @author flogyin
 */
public class SimpleDataSet implements DataSet, java.io.Serializable {
	private static final long		serialVersionUID	= 1L;

	protected String				name;
	protected DataItemCollection	dataItems;

	public SimpleDataSet() {
		this.dataItems = new SimpleDataItemCollection();
	}

	public SimpleDataSet(DataItemCollection dataItems) {
		this.dataItems = dataItems;
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public DataItemCollection getResult() {
		return this.dataItems;
	}

	@Override
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String toJson() {
		LinkedHashMap<String, String> itemMap = new LinkedHashMap<String, String>();
		DataColumnCollection itemDataColumnCollection = getResult().getColumns();
		int itemCount = itemDataColumnCollection.getCount();
		for (int i = 0; i < itemCount; i++) {
			DataColumn itemDataColumn = itemDataColumnCollection.get(i);
			String colName = itemDataColumn.getColumn().getName();
			if (colName == null || colName.length() == 0) {
				colName = this.name != null && this.name.length() > 0 ? this.name : "DataSet-return";
			} else {
				String lColName = colName.toLowerCase();
				if (colName.equals(lColName) || colName.equals(colName.toUpperCase()))
					colName = lColName;
			}
			String value = itemDataColumn.asString();
			itemMap.put(colName, value);
		}

		DataTableCollection tableCollection = getResult().getTables();
		int tableCount = tableCollection.getCount();

		/*
		 * if (tableCount == 1 && itemCount == 0) { return
		 * toJson(tableCollection.get(0)); }
		 */

		LinkedHashMap tableMap = new LinkedHashMap();
		for (int t = 0; t < tableCount; t++) {
			DataTable table = tableCollection.get(t);
			String tabName = table.getName();
			String lTabName = tabName.toLowerCase();
			if (tabName.equals(lTabName) || tabName.equals(tabName.toUpperCase())) {
				tabName = lTabName;
			}
			ArrayList<LinkedHashMap<String, String>> rowList = new ArrayList<LinkedHashMap<String, String>>();
			tableMap.put(tabName, rowList);

			DataRowCollection rows = table.getRows();
			int rowsCount = rows.getCount();
			for (int r = 0; r < rowsCount; r++) {
				LinkedHashMap<String, String> rowMap = new LinkedHashMap<String, String>();
				rowList.add(rowMap);

				DataRow row = rows.get(r);
				int colCount = row.getCount();
				for (int c = 0; c < colCount; c++) {
					DataColumn dataColumn = row.get(c);
					String colName = dataColumn.getColumn().getName();
					String lColName = colName.toLowerCase();
					String value = dataColumn.asString();
					rowMap.put((colName.equals(lColName) || colName.equals(colName.toUpperCase())) ? lColName : colName, value);
				}
			}
		}

		if (itemCount > 0) {
			tableMap.put("_result", itemMap);
		}
		return JSON.toJSONString(tableMap);
	}

	/*
	 * public String toJson_rich() { HashMap<String,String> itemMap = new
	 * HashMap<String,String>(); DataColumnCollection itemDataColumnCollection =
	 * getResult().getColumns(); int itemCount =
	 * itemDataColumnCollection.getCount(); for (int i = 0; i < itemCount; i++)
	 * { DataColumn itemDataColumn = itemDataColumnCollection.get(i); String
	 * name = itemDataColumn.getColumn().getName(); String value =
	 * itemDataColumn.asString(); itemMap.put(name, value); }
	 * 
	 * DataTableCollection tableCollection = getResult().getTables(); int
	 * tableCount = tableCollection.getCount();
	 * 
	 * HashMap<String,ArrayList<String>> columnMap = new
	 * HashMap<String,ArrayList<String>>(); for (int t = 0; t < tableCount; t++)
	 * { DataTable table = tableCollection.get(t); ArrayList<String> columnList
	 * = new ArrayList<String>(); columnMap.put(table.getName(), columnList);
	 * 
	 * ColumnCollection columns = table.getColumns(); int columnCount =
	 * columns.getCount(); for (int c = 0; c < columnCount; c++) { String name =
	 * columns.get(c).getName(); columnList.add(name); } }
	 * 
	 * HashMap<String,ArrayList<HashMap<String,String>>> tableMap = new
	 * HashMap<String,ArrayList<HashMap<String,String>>>(); for (int t = 0; t <
	 * tableCount; t++) { DataTable table = tableCollection.get(t);
	 * ArrayList<HashMap<String,String>> rowList = new
	 * ArrayList<HashMap<String,String>>(); tableMap.put(table.getName(),
	 * rowList);
	 * 
	 * DataRowCollection rows = table.getRows(); int rowsCount =
	 * rows.getCount(); for (int r = 0; r < rowsCount; r++) {
	 * HashMap<String,String> rowMap = new HashMap<String,String>();
	 * rowList.add(rowMap);
	 * 
	 * DataRow row = rows.get(r); int colCount = row.getCount(); for (int c = 0;
	 * c < colCount; c++) { DataColumn dataColumn = row.get(c); String name =
	 * dataColumn.getColumn().getName(); String value = dataColumn.asString();
	 * rowMap.put(name, value); } } }
	 * 
	 * HashMap<String,HashMap> jsonMap = new HashMap<String,HashMap>(); if
	 * (itemCount > 0) { jsonMap.put("result", itemMap); }
	 * jsonMap.put("columns", columnMap); jsonMap.put("rows", tableMap); return
	 * JSON.toJSONString(jsonMap); }
	 */

	@Override
	public String toXml() {
		StringBuilder builder = new StringBuilder();
		builder.append("<root>");

		DataColumnCollection dataColumnCollection = getResult().getColumns();
		int count = dataColumnCollection.getCount();
		for (int i = 0; i < count; i++) {
			DataColumn dataColumn = dataColumnCollection.get(i);
			String name = dataColumn.getColumn().getName();
			String value = dataColumn.getString();
			builder.append("<");
			builder.append(name);
			builder.append(">");
			builder.append(value);
			builder.append("</");
			builder.append(name);
			builder.append(">");
		}

		DataTableCollection dataTableCollection = getResult().getTables();
		count = dataTableCollection.getCount();
		if (count > 0) {
			builder.append("<DataTable>");
			for (int i = 0; i < count; i++) {
				builder.append(dataTableCollection.get(i).toXml());
			}
			builder.append("</DataTable>");
		}

		builder.append("</root>");

		return builder.toString();
	}

	@Override
	public void clear() {
		this.name = null;
		this.dataItems.clear();
	}
}
